﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using ShadowCommando.Game.Utilites;
using System.Configuration;
using ShadowCommando.Game.Elements.Bulllets;
using ShadowCommando.Game.GameSounds;
namespace ShadowCommando.Game.Elements
{
    /// <summary>
    /// Klasa reprezentująca pocisk
    /// </summary>
   public class Bullet:Element
    {
       /// <summary>
       /// Konstruktor 
       /// </summary>
       /// <param name="_step">Krok</param>
       /// <param name="_diff">Poziom trudności</param>
       /// <param name="_owner">Owner</param>
       /// <param name="_bulletId">Id pocisku</param>
        public Bullet(int _step,LevelDifficulty _diff,ElementId _owner,BulletId _bulletId):base(_step,_diff)
        {
            difficulty = _diff;
            ElementId = ElementId.Bullet;
            straight = new Straight();
            bulletId = _bulletId;
            timerCount = 5;//dodac do resource
            step = _step;
            owner = _owner;
            zIndex = 1;
            SetSound();
            Create(bulletId);
        }
        #region Elements

        private ElementId owner;
        private BulletId bulletId;
        private Straight straight;
        private int demage;
        private int speed;
        private int distance;
        private int dx, dy, fx, fy;
        private double distX;
        private double distY;
        private int radius;

        #endregion
        #region Properties
        /// <summary>
        /// ElementId postaci która wystrzeliła pocisk
        /// </summary>
        public ElementId Owner
        {
            get
            {
                return owner;
            }
            set
            {
                owner = value;
            }
        }
       /// <summary>
       /// Id pocisku
       /// </summary>
        public BulletId BulletId
        {
            get
            {
                return bulletId;
            }
            set
            {
                bulletId = value;
            }
        }
        /// <summary>
        /// Obrazenia zadawane przez pocisk
        /// </summary>
        public int Demage
        {
            get
            {
                return demage;
            }
            set
            {
                demage = value;
            }
        }
        /// <summary>
        /// Szybkość pocisku
        /// </summary>
        public int Speed
        {
            get
            {
                return speed;
            }
            set
            {
                speed = value;
            }
        }
       /// <summary>
       /// Zasięg pocisku
       /// </summary>
        public int Distance
        {
            get
            {
                return distance;
            }
            set
            {
                distance = value;
            }
        }
       /// <summary>
       /// Promień obrazeń
       /// </summary>
        public int Radius
        {
            get { return radius; }
            set { radius = value; }
        }
        #endregion
        #region Functions
       /// <summary>
       /// Ustaw prostą
       /// </summary>
       /// <param name="p1">Punkt p1</param>
       /// <param name="_cos">Cosinus kąta</param>
       /// <param name="_sin">Sinus kąta</param>
        public void SetStraight(Point p1, double _cos, double _sin)
        {
            fx = FakeX;
            fy = FakeY;
            dx = p1.X;
            dy = p1.Y;
            cos = _cos;
            sin = _sin;
            straight.Set(fakeLocation.X, fakeLocation.Y, p1.X, p1.Y);
            double wsp = speed;
            double ywsp = wsp;
            if (fx < dx)
            {
                wsp = -wsp;
            }
            double a = cos * wsp;
            distX = a;
            if ((int)distX != 0)
            {
                distY = straight.F(distX + FakeX) - straight.F(FakeX);
            }
            else
            {
                distY = wsp;
                if (fy < dy)
                    distY = Math.Abs(distY);
                else
                    distY = -Math.Abs(distY);
                //distX = 0; 
                return;
            }
            double d;
            d = Math.Sqrt(Math.Pow(fx - (FakeX + distX), 2) + Math.Pow(fy - (distY + FakeY), 2));
            while (!(d > speed - 1 && d < speed + 1))
            {
                d = Math.Sqrt(Math.Pow(fx - (FakeX + distX), 2) + Math.Pow(fy - (distY + FakeY), 2));
                if (d < speed)
                {
                    wsp = (Math.Abs(wsp) + 0.1) * ((wsp) / Math.Abs(wsp));
                }
                else
                {
                    wsp = (Math.Abs(wsp) - 0.1) * ((wsp) / Math.Abs(wsp));

                }
                distY = sin * wsp;
            }


        }
        /// <summary>
        /// Ruch pocisku
        /// </summary>
        /// <returns></returns>
        public bool Move()
        {
            FakeX += (int)distX;
            FakeY += (int)distY;
            distance--;

            return distance != 0 ? true : false;
        }
        /// <summary>
        /// Explozja pocisku
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public bool MakeExplosion(Element element)
        {
            Point p1 =new Point();
            p1.X= element.FakeX + element.Width/2;
            p1.Y = element.FakeY + element.Height/2;
            
            Point p2=new Point();
            p2.X = FakeX + width/2;
            p2.Y = FakeY + height/2;

            return radius > (Math.Sqrt(Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2))) ? true : false;

        }
        private void Create(BulletId id)
        {
            switch (id)
            {
                case Elements.BulletId.Laser:
                    {
                        LoadSettings(LaserInfo.Default);
                        break;
                    }
                case Elements.BulletId.Bomb:
                    {
                        LoadSettings(BombInfo.Default);
                        break;
                    }
                case Elements.BulletId.Flechette:
                    {
                        LoadSettings(FlechetteInfo.Default);
                        break;
                    }
                case BulletId.Check:
                    {
                        LoadSettings(CheckBullet.Default);
                        break;
                    }
                case BulletId.HeavyLaser:
                    {
                        LoadSettings(HeavyLaserInfo.Default);
                        break;
                    }
            }
        }
        private void LoadSettings(ApplicationSettingsBase asb)
        {
            demage =(int) asb["Demage"];
            speed =(int) asb["Speed"];
            radius = (int) asb["Radius"];
            distance = (int) asb["Distance"];
            IsAnimated = (bool) asb["IsAnimated"];
            timerCount = (int) asb["TimerCount"];
        }
        private void SetSound()
        {
            switch (bulletId)
            {
               
                default:
                    {
                        soundiD = SoundID.Shot1;
                        break;
                    }
            }
        }
        #endregion
    }
}
